package com.example.zhangxin.startproject_124zx.model;

import com.example.zhangxin.startproject_124zx.bean.WeatherResult;
import com.example.zhangxin.startproject_124zx.iface.WeatherIface;
import com.example.zhangxin.startproject_124zx.iface.WeatherListener;
import com.example.zhangxin.startproject_124zx.service.WeatherServie;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by zhangxin on 2017/3/14.
 */

public class Weather51Model implements WeatherIface {

    private Retrofit retrofit;
    private  String BASEURL="http://weather.51wnl.com/weatherinfo/";
    private WeatherServie weatherServie;

    public Weather51Model(){
        //Retrofit使用3-1
        retrofit=new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    @Override
    public void getWeather(String cityno, final WeatherListener Listener) {
        //Retrofit使用3-2
        weatherServie=retrofit.create(WeatherServie.class);
        Call<WeatherResult> call=weatherServie.getResult(cityno);


        //Retrofit使用3-3
        call.enqueue(new Callback<WeatherResult>() {
            @Override//访问网络成功
            public void onResponse(Call<WeatherResult> call, Response<WeatherResult> response) {
                if (response.isSuccessful() && response.body()!=null)
                    Listener.onResponse(response.body().getWeatherinfo());
                else
                    Listener.onFail("onresponse fail");
            }
            @Override
            public void onFailure(Call<WeatherResult> call, Throwable t) {
                Listener.onFail(t.toString());
            }
        });
    }
}


